<template>
	<!-- 首页轮播广告、公告-添加 -->
	<div>
		<MyNavbar :title="navTitle">
		</MyNavbar>
		
		<div class="base-box">
			<u-form :model="form" labelPosition="top" labelWidth="100%">
				<u-form-item label="封面" v-if="adType == 1">
					<MyImgUpload v-model="form.poster"/>
				</u-form-item>
				<u-form-item label="标题">
					<u-input v-model="form.title" clearable placeholder="标题"/>
				</u-form-item>
				<u-form-item  label="排序,越小越靠前">
					<u-input v-model="form.sortNo" type="number" clearable placeholder="排序,越小越靠前"/>
				</u-form-item>
				<u-form-item  label="视频链接">
					<u-textarea v-model="form.url" clearable placeholder="视频链接"/>
				</u-form-item>
				
				<div class="flex-column">
					<u-button type="primary" :loading="loading" text="保 存" @click="onClickSubmit"></u-button>
					<div style="height: 24rpx;"></div>
					<u-button v-if="operType==='edit'" type="error" plain :loading="loading" text="删 除" @click="onClickDelete"></u-button>
				</div>
			</u-form>
		</div>
		
	</div>
</template>

<script>
	import MyImgUpload from '@/components/form/MyImgUpload.vue';
	import {mapState, mapMutations} from 'vuex';
	
	export default {
		components: {
			MyImgUpload,
		},
		data() {
			return {
				loading: false,
				// 类型，1：banner，2：公告
				adType: "1",
				// 操作类型，add：添加，edit：修改
				operType: 'add',
				// 表单
				form: {
					

				},
			}
		},
		computed: {
			...mapState('admin', {
				currentShopUid: 'currentShopUid',
			}),
			navTitle(){
				let {operType, adType} = this;
				let operName = "";
				if (operType === 'add') {
					operName = '添加';
				} else {
					operName = '编辑';
				}
				let adTypeName = "";
				if (adType == 2) {
					adTypeName = "公告";
				} else {
					adTypeName = "首页轮播广告";
				}
				return adTypeName + "-" + operName;
			},
		},
		onLoad(option) {
			this.adType = option.adType || "1";
			this.operType = option.operType || "add";
			this.initRowInfo(option.uid);
		},
		methods: {
			/**
			 * 初始化表单信息
			 */
			initRowInfo(uid){
				if (!uid) {
					return;
				}
				this.$api.banner.get({uid: uid}).then(({data})=>{
					this.form = data;
				});
			},
			// 点击保存
			onClickSubmit(){
				let form = this.form;
				form.shopUid = this.currentShopUid;
				form.adType = this.adType;
				this.loading = true;
				this.$api.banner.save(form).then(res=>{
					this.$utils.showSuccess('保存成功');
					uni.navigateBack();
					this.loading = false;
				}).catch(e=>{
					this.loading = false;
				});
			},
			// 点击删除
			onClickDelete(){
				uni.showModal({
					title: "确定删除？",
					success: (res)=>{
						if (res.confirm !== true) {
							return;
						}
						let {form} = this;
						this.$api.banner.del(form).then(res=>{
							this.$utils.showSuccess('删除成功');
							uni.navigateBack();
						}).catch(e=>{
						});
					},
				})
			},
		},
		
	}
</script>

<style scoped>
	
</style>
